<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        //今日矿机产量
        $today_input = Db::name('user_output_log')->whereTime('createtime','d')->sum('output');
    
        //今日社区奖励来源:1=币币交易买入,2=币币交易卖出,3=OTC通道买入,4=OTC通道卖出,5=矿机收益,6=动态奖励,7=购买矿机,8=激活矿机,9=撤销订单,10=空投
        $today_she = Db::name('user_balance_log')->where('source',6)->whereTime('createtime','d')->sum('amount');
        //今日已领取矿机奖励
        $today_input_get = Db::name('user_output_log')->where('status',1)->whereTime('createtime','d')->sum('output');
        $total_user = Db::name('user')->where('status','normal')->count('id');
        $today_user = Db::name('user')->where('status','normal')->whereTime('createtime','d')->count('id');
        $name_user = Db::name('user')->where('status','normal')->where('verify_status',1)->count('id');
        $live_user = Db::name('user')->where('status','normal')->where('active_status',1)->count('id');

        //OTC充值
        $otc_recharge_num = Db::name('user_order')->where(['order_type'=>2])->count('id');
        $otc_recharge = Db::name('user_order')->where(['order_type'=>2])->sum('coin_num');
        //OTC充值成功
        $otc_recharge_success_num = Db::name('user_order')->where(['order_status'=>1,'order_type'=>2])->count('id');
        $otc_recharge_success = Db::name('user_order')->where(['order_status'=>1,'order_type'=>2])->sum('coin_num');

        //OTC卖出
        $otc_exchange_num = Db::name('user_order')->where(['order_type'=>3])->count('id');
        $otc_exchange = Db::name('user_order')->where(['order_type'=>3])->sum('coin_num');
        //OTC卖出成功
        $otc_exchange_success_num = Db::name('user_order')->where(['order_type'=>3,'order_status'=>1])->count('id');
        $otc_exchange_success = Db::name('user_order')->where(['order_type'=>3,'order_status'=>1])->sum('coin_num');

        //币币交易总量
        $coin_num = Db::name('user_order')->where(['order_type'=>1])->count('id');
        //币币交易总额
        $coin_total = Db::name('user_order')->where(['order_type'=>1])->sum('coin_num');
        //币币交易成功交易总量
        $coin_num_success = Db::name('user_order')->where(['order_status'=>1,'order_type'=>1])->count('id');
        //币币交易成功交易总额
        $coin_total_success = Db::name('user_order')->where(['order_status'=>1,'order_type'=>1])->sum('coin_num');

        //正在运行的矿机
        $runing_miner = Db::name('user_miner')->where(['status'=>1])->count('id');
        $wait_miner = Db::name('user_miner')->where(['status'=>2])->count('id');
        $today_add_miner = Db::name('user_miner')->whereTime('createtime', 'd')->where('miner_id','gt',0)->count('id');
        $today_coin_miner = Db::name('user_balance_log')->whereTime('createtime', 'd')->where('source',5)->sum('amount');
        $mu_miner = Db::name('user_miner')->where(['miner_cat'=>1,'status'=>1])->count('id');
        $mq_miner = Db::name('user_miner')->where(['miner_cat'=>2,'status'=>1])->count('id');
        // $xin = Db::name('user_miner')->where(['status'=>1,'miner_cat'=>1])->count('id');
        // $fu = Db::name('user_miner')->where(['status'=>1,'miner_cat'=>2])->count('id');
        // $fil = Db::name('user_miner')->where(['status'=>1,'miner_cat'=>3])->count('id');
        // $xian = Db::name('user_miner')->where(['status'=>1,'miner_cat'=>4])->count('id');
        // $jiang = Db::name('user_miner')->where(['status'=>1,'miner_cat'=>5])->count('id');

        // //今日新增矿机
        // $ti_today = Db::name('user_miner')->where(['miner_cat'=>0])->whereTime('createtime','d')->count('id');
        // $xin_today = Db::name('user_miner')->where(['miner_cat'=>1])->whereTime('createtime','d')->count('id');
        // $fu_today = Db::name('user_miner')->where(['miner_cat'=>2])->whereTime('createtime','d')->count('id');
        // $fil_today = Db::name('user_miner')->where(['miner_cat'=>3])->whereTime('createtime','d')->count('id');
        // $xian_today = Db::name('user_miner')->where(['miner_cat'=>4])->whereTime('createtime','d')->count('id');
        // $jiang_today = Db::name('user_miner')->where(['miner_cat'=>5])->whereTime('createtime','d')->count('id');

        $this->view->assign([
            'totalminer'       => Db::name('user_miner')->where('status','1')->count('id'),
            'totalorder'       => Db::name('user_order')->count('id'),
            'otc_recharge_num'=>$otc_recharge_num,
            'otc_recharge'=>$otc_recharge,
            'otc_recharge_success_num'=>$otc_recharge_success_num,
            'otc_recharge_success'=>$otc_recharge_success,
            'otc_exchange_num'=>$otc_exchange_num,
            'otc_exchange'=>$otc_exchange,
            'otc_exchange_success_num'=>$otc_exchange_success_num,
            'otc_exchange_success'=>$otc_exchange_success,
            'coin_num'=>$coin_num,
            'coin_total'=>$coin_total,
            'coin_num_success'=>$coin_num_success,
            'coin_total_success'=>$coin_total_success,

            'runing_miner'=>$runing_miner,
            'wait_miner'=>$wait_miner,
            'today_add_miner'=>$today_add_miner,
            'mu_miner'=>$mu_miner,
            'mq_miner'=>$mq_miner,
            'today_coin_miner'=>$today_coin_miner,

            'total_pay_mu' => Db::name('user')->where('status','normal')->sum('pay_mu'),

            'total_active_mu'  => Db::name('user')->where('status','normal')->sum('active_mu'),

            'total_mq'  => Db::name('user')->where('status','normal')->sum('mq'),
            
            'sevendnu'         => '80%',
            'sevendau'         => '32%',
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'       => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);
        return $this->view->fetch();
    }

}
